package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.pojo.Stock;
import com.example.pojo.StockFreezeTcc;
import com.example.service.StockFreezeTccService;
import com.example.service.StockService;
import com.example.service.StockTccService;
import io.seata.core.context.RootContext;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

//@Service
public class StockTccServiceImpl implements StockTccService 
{
	@Autowired
	StockService stockService;
	@Autowired
	StockFreezeTccService stockFreezeTccService;

	@Override
	@Transactional
	public void reduce(Integer productId, Integer count) {

		QueryWrapper<StockFreezeTcc> qw = new QueryWrapper<>();
		String xid = RootContext.getXID();
		qw.eq("xid",xid);
		StockFreezeTcc stockFreezeTcc = stockFreezeTccService.getOne(qw);
		if(stockFreezeTcc != null)
			return;
		
		//1.库存判断
		Integer amount = stockService.getById(productId).getAmount();
		if(amount<count || amount == 0)
		{
//			return false;
//			throw new RuntimeException("库存不足");
			return;
//			return false;
		}
		//2.扣减可用库存
		
		stockService.reduce(productId,count);
		
		//3.记录冻结库存
		stockFreezeTcc = new StockFreezeTcc();
		stockFreezeTcc.setXid(xid);//设置当前事务
		stockFreezeTcc.setStockId(productId);
		stockFreezeTcc.setStatus(StockFreezeTcc.STATUS.PREPARE);
		stockFreezeTcc.setFrozenCount(count);
		stockFreezeTccService.save(stockFreezeTcc);

	}

	@Override
//	@Transactional
	public boolean confirm(BusinessActionContext context) {
//		log.info("Confirm阶段完整上下文: {}", context.getActionContext()); // 检查Map内容
		if(true)
			return true;
		// 1. 获取Try阶段参数
		Integer productId = (Integer) context.getActionContext("productId");
		Integer count = (Integer) context.getActionContext("count");
		
		// 2. 确认预留（Confirm阶段）
		QueryWrapper<StockFreezeTcc> qw = new QueryWrapper<>();
		String xid = context.getXid();
		qw.eq("xid",xid);

		//设置完成状态，清除冻结库存
		StockFreezeTcc one = stockFreezeTccService.getOne(qw);
		if (one == null) {
//			throw new IllegalStateException("冻结记录不存在，可能 Try 阶段未执行成功");
			return false;
		}
//		one.setStatus(StockFreezeTcc.STATUS.CONFIRM);
//		one.setFrozenCount(0);
//		stockFreezeTccService.updateById(one);
		stockFreezeTccService.removeById(one);
		return true;
	}

	@Override
//	@Transactional
	public boolean cancel(BusinessActionContext context) 
	{
		if(true)
			return true;
		// 1. 获取Try阶段参数
//		int productId = (Integer)context.getActionContext("productId");
//		int count = (Integer)context.getActionContext("count");
		// 从 context 中获取 Try 阶段的参数
		Integer productId = (Integer) context.getActionContext("productId");
		Integer count = (Integer) context.getActionContext("count");

		// 2. 释放预留（Cancel阶段）

		QueryWrapper<StockFreezeTcc> qw = new QueryWrapper<>();
		qw.eq("xid",context.getXid());
		StockFreezeTcc one = stockFreezeTccService.getOne(qw);
		
		//空判断
		if(one == null)
		{
			StockFreezeTcc stockFreezeTcc = new StockFreezeTcc();
			stockFreezeTcc.setXid(RootContext.getXID());//设置当前事务
			stockFreezeTcc.setStockId(productId);
			stockFreezeTcc.setStatus(StockFreezeTcc.STATUS.CANCEL);
			stockFreezeTcc.setFrozenCount(count);
			stockFreezeTccService.save(stockFreezeTcc);
		}
		
		//幂等
		if(one.getStatus() == StockFreezeTcc.STATUS.CANCEL)
			return true;
		
		
		// 恢复库存
		Stock stock = stockService.getById(one.getStockId());
		Integer amount = stock.getAmount();
		amount+=count;
		stock.setAmount(amount);
		stockService.updateById(stock);
		//清除冻结库存	设置cancel状态
		one.setFrozenCount(0);
		one.setStatus(StockFreezeTcc.STATUS.CANCEL);
		stockFreezeTccService.updateById(one);
		return true;
	}
}
